package com.fancyy.scm.activity;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.databinding.ObservableBoolean;
import android.databinding.ObservableField;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.fancyy.scm.R;
import com.fancyy.scm.base.BaseActivity;
import com.fancyy.scm.base.annotation.Layout;
import com.fancyy.scm.base.annotation.Translucent;
import com.fancyy.scm.base.http.BRDataListener;
import com.fancyy.scm.base.http.BRDataTransport;
import com.fancyy.scm.bean.HttpMessage;
import com.fancyy.scm.bean.ScanResult;
import com.fancyy.scm.bean.UserInfo;
import com.fancyy.scm.databinding.ActivityLoginBinding;
import com.uuzuche.lib_zxing.activity.CaptureActivity;
import com.uuzuche.lib_zxing.activity.CodeUtils;

import static com.fancyy.scm.BRConstant.API_LOGIN;
import static com.fancyy.scm.BRConstant.API_LOGIN_1;
import static com.fancyy.scm.BRConstant.SCANNING;

@Layout(R.layout.activity_login)
@Translucent
public class LoginActivity extends BaseActivity<ActivityLoginBinding> {
    public ObservableField<String> name = new ObservableField<>();
    public ObservableField<String> pwd = new ObservableField<>();
    public ObservableBoolean rm = new ObservableBoolean(true);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setNeedScan(true);
        super.onCreate(savedInstanceState);
        SharedPreferences sp = getSharedPreferences("set", MODE_PRIVATE);
        name.set(sp.getString("name", null));
        pwd.set(sp.getString("pwd", null));
        rm.set(sp.getBoolean("rm", true));
    }

    public void onLoginClick(View view) {
        if (TextUtils.isEmpty(name.get())) {
            showToast("User cannot be empty");
            return;
        }
        if (TextUtils.isEmpty(pwd.get())) {
            showToast("Password cannot be empty");
            return;
        }
        doLogin();
    }

    private void doLogin() {
        BRDataTransport.create(API_LOGIN)
                .addParam("UserAccount", name.get())
                .addParam("Password", pwd.get())
                .addProgressing(this, getString(R.string.Progressing))
                .execute(new BRDataListener<UserInfo>() {
                    @Override
                    public void onSuccess(UserInfo data) {
                        handleLoginResult(data);
                    }
                }, UserInfo.class);
    }

    public void onScanningClick(View view) {
        //if(!getPDA()) {
            Intent intent = new Intent(this, CaptureActivity.class);
            startActivityForResult(intent, 1);
        //}
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 1 && resultCode == RESULT_OK) {
            String code = data.getStringExtra(CodeUtils.RESULT_STRING);
            handleCode(code);
        }
    }

    private void handleCode(String code) {
        BRDataTransport.create(API_LOGIN_1)
                .addParam("UserAccount", code)
                .addParam("IsScanUser", 1)
                .addProgressing(self(), getString(R.string.Progressing))
                .execute(new BRDataListener<UserInfo>() {
                    @Override
                    public void onSuccess(UserInfo data) {
                        handleLoginResult(data);
                    }
                }, UserInfo.class);
    }

    private void handleLoginResult(UserInfo data) {
        //
        SharedPreferences sp = getSharedPreferences("set", MODE_PRIVATE);
        if (rm.get()) {
            sp.edit().putString("name", name.get())
                    .putString("pwd", pwd.get())
                    .putBoolean("rm", true)
                    .commit();
        } else {
            sp.edit().putString("name", "")
                    .putString("pwd", "")
                    .putBoolean("rm", false)
                    .commit();
        }
        getBRApplication().setUserInfo(data);
        //
        startActivity(MainActivity.class);
    }

    @Override
    public void doScan(String data) {
        handleCode(data);
    }
}
